#include "mylabel.h"
#include <QMouseEvent>
#include <QPainter>
#include"setrect.h"
#include<qdebug.h>

mylabel::mylabel(QWidget *parent):QLabel(parent)
{
  this->setMouseTracking(true);
  this->setAttribute(Qt::WA_TranslucentBackground);
  this->setStyleSheet("font-size:40px;color:red");
  this->setAlignment(Qt::AlignLeft|Qt::AlignTop);
  this->x=0;
  this->y=0;
  this->w=0;
  this->h=0;
  this->lp=false;
  this->rects[0][0]=0;
}
mylabel::~mylabel()
{}

void mylabel::mouseMoveEvent(QMouseEvent *event)
{
  QString str;
  if(true==this->lp){
    this->w=event->x() - this->x;
    this->h=event->y() - this->y;
      str=QString("%3x%4+%1+%2").arg(this->x).arg(this->y).arg(this->w).arg(this->h);
    }else{
      str=QString("%1+%2").arg(event->x()).arg(event->y());
    }
  str+="  Press mouse right button to exit.";
  this->setText(str);
}
void mylabel::mousePressEvent(QMouseEvent *event)
{
  if(event->button()==Qt::LeftButton){
    this->lp=true;
    this->x=event->x();
    this->y=event->y();
  }
  if(event->button()==Qt::RightButton){
    this->hide();
    QString str= QString::number(this->x)+","+QString::number(this->y)+","+QString::number(this->w)+","+QString::number(this->h);
    if(nullptr!=this->sr){
            this->sr->showNormal();
            if(nullptr!=this->sr->cle)this->sr->cle->setText(str);
    }
  }
}
void mylabel::mouseReleaseEvent(QMouseEvent *event)
{
  if(event->button()==Qt::LeftButton)
  {
    this->lp=false;
  }
}
void mylabel::paintEvent(QPaintEvent *event)
{
  QLabel::paintEvent(event);
  if(true==this->lp){
    QPainter painter(this);
    painter.setPen(QPen(Qt::green,2));
    painter.drawRect(this->x,this->y,this->w,this->h);
  }
  if(rects[0][0]>0){
      QPainter painter(this);
      painter.setPen(QPen(Qt::yellow,4));
      for(int i=1;i<=rects[0][0];i++){
        painter.drawRect(rects[i][0],rects[i][1],rects[i][2],rects[i][3]);
//        qDebug()<<rects[i][0]<<rects[i][1]<<rects[i][2]<<rects[i][3]<<endl;
      }
  }
}
